package com.example.springsecuritywithdatabase.service;

import com.example.springsecuritywithdatabase.bean.UserInfo;
import com.example.springsecuritywithdatabase.mapper.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    public UserInfo getUserInfoByUsername(String username) {
        UserInfo userInfo = userInfoMapper.getUserInfoByUsername(username);
        System.out.println("userInfo: " + userInfo);
        String s = passwordEncoder.encode("1234");
        System.out.println("测试相同密码加密结果是否相同:\n"
                + passwordEncoder.encode("1234") +
                "\n" + passwordEncoder.encode("1234") +
                " " + passwordEncoder.matches("1234", s));
        return userInfo;
    }

    public int insertUser(UserInfo userInfo) {
        // 加密密码
        userInfo.setPassword(passwordEncoder.encode(userInfo.getPassword()));
        return userInfoMapper.insertUserInfo(userInfo);
    }
}
